#include <iostream>
#include <stdio.h>
#include <vector>

using namespace std;
//抽象观察者MyObserver
class MyObserver{
public:
	virtual void response(){};
};

//抽象目标类MySubject
class MySubject{
public:
	//注册 
	void attach(MyObserver *observer){
		observers.push_back(observer);
	}
	
	//注销
	void detach(MyObserver *observer){
		for(auto it = observers.begin() ; it!=observers.end() ; it++){
			if(*it == observer){
				observers.erase(it);
				break;
			}
		}
	}
protected:
	vector<MyObserver*> observers;
}; 

//具体目标类Cat（猫类）
class Cat : public MySubject{
public:
	void cry(){
		cout << "猫叫!" << endl;
		for(auto obs : observers){
			obs->response();
		}
	}
}; 


//具体观察者类Mouse（老鼠类）
class Mouse : public MyObserver{
public:
	void response(){
		cout << "老鼠逃跑!" << endl;
	}
}; 

//具体观察者类Dog（狗类）
class Dog : public MyObserver{
public:
	void response(){
		cout << "狗叫!" << endl;
	}
}; 



int main(int argv, char**argc)
{
    printf("************  test hello zc!!!   **************\n");
    /* add test code! */

	//创建目标 
	Cat cat;
	
	//创建观察者 
	Mouse m1,m2;
	Dog dog;
	
	//订阅 
	cat.attach(&m1);
	cat.attach(&m2);
	cat.attach(&dog);
	
	//更新 
	cat.cry();
	
    
    return 0;
}
